<!--
 * @Author: your name
 * @Date: 2021-09-06 15:47:05
 * @LastEditTime: 2021-09-09 14:43:23
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ej-webui\src\pages\customer\index.vue
-->
<template>
  <div>
    <el-form :inline="true" :model="params" class="demo-form-inline">
      <el-form-item>
        <el-button type="primary" size="small" @click="addHandler"
          >添加</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.username"
          placeholder="请输入用户名"
          size="small"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="params.status"
          placeholder="请选择状态"
          size="small"
          clearable
        >
          <el-option label="正常" value="正常"></el-option>
          <el-option label="禁用" value="禁用"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="pageQueryEmployees()"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <el-table style="width: 100%" :data="resData.list" size="small">
      <el-table-column label="编号" width="100" prop="id"> </el-table-column>
      <el-table-column label="用户名" width="180" prop="username">
      </el-table-column>
      <el-table-column label="姓名" width="230" prop="realname">
      </el-table-column>
      <el-table-column label="手机号" width="180" prop="telephone">
      </el-table-column>
      <el-table-column label="性别" width="180" prop="gender">
        <template v-slot="scope">
          <i v-if="scope.row.gender == 'male'" class="el-icon-male"></i>
          <i v-else-if="scope.row.gender == 'female'" class="el-icon-female"></i>
          <span v-else>暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="180" prop="status">
        <template v-slot="scope">
          <el-tag
            v-if="scope.row.status == '正常'"
            size="mini"
            type="success"
            >{{ scope.row.status }}</el-tag
          >
          <el-tag v-else size="mini" type="danger">{{
            scope.row.status
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button type="text" @click="changeTable(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            @click="deleteTable(scope.row.id)"
            style="color: red"
            >删除</el-button
          >
          <!-- <el-button type="text" @click="juspHandler">详情</el-button> -->
          <el-button
            type="text"
            @click="juspTable(scope.row.id)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="float: right; margin-top: 10px"
      layout="total,sizes,prev, pager, next,jumper"
      :total="resData.total"
      :page-size="params.pageSize"
      :current-page="params.page"
      @current-change="pageChangeHandler"
      :page-sizes="[5, 10, 15]"
      @size-change="sizeChangeHandler"
      size="small"
    >
    </el-pagination>
    <!-- 保存模态框 -->
    <el-dialog
      :title="title"
      :visible.sync="visible1"
      @close="dialogCloseHandler"
    >
      <el-form :model="employees" :rules="rules" ref="saveForm">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input
            v-model="employees.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="姓名"
          :label-width="formLabelWidth"
          prop="realname"
        >
          <el-input
            v-model="employees.realname"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="手机号"
          :label-width="formLabelWidth"
          prop="telephone"
        >
          <el-input
            v-model="employees.telephone"
            placeholder="请输入手机号"
          ></el-input> </el-form-item
        ><el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input
            v-model="employees.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="性别"
          :label-width="formLabelWidth"
          size="mini"
          prop="gender"
        >
          <el-radio v-model="employees.gender" label="male">男</el-radio>
          <el-radio v-model="employees.gender" label="female">女</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible1 = false">取 消</el-button>
        <el-button type="primary" @click="toSaveHandler">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑模态框 -->
    <el-dialog
      :title="title"
      :visible.sync="visible2"
      @close="dialogCloseTable"
    >
      <el-form :model="employees" :rules="rules" ref="saveForm2">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input
            v-model="employees.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="姓名"
          :label-width="formLabelWidth"
          prop="realname"
        >
          <el-input
            v-model="employees.realname"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="手机号"
          :label-width="formLabelWidth"
          prop="telephone"
        >
          <el-input
            v-model="employees.telephone"
            placeholder="请输入手机号"
          ></el-input> </el-form-item
        ><el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input
            type="password"
            v-model="employees.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="性别"
          :label-width="formLabelWidth"
          size="mini"
          prop="gender"
        >
          <el-radio v-model="employees.gender" label="male">男</el-radio>
          <el-radio v-model="employees.gender" label="female">女</el-radio>
        </el-form-item>
        <el-form-item
          label="状态"
          :label-width="formLabelWidth"
          size="mini"
          prop="status"
        >
          <el-radio v-model="employees.status" label="正常">正常</el-radio>
          <el-radio v-model="employees.status" label="禁用">禁用</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible2 = false">取 消</el-button>
        <el-button type="primary" @click="toSaveTable">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request"
import qs from 'qs'
import _ from 'lodash'  //克隆
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 5,
        roleId: 6
      },
      resData: [],
      title: '',
      visible1: false,
      visible2: false,
      employees: {},
      formLabelWidth: '80px',
      imageUrl: null,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        realname: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        telephone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //设置为异步函数
    async pageQueryEmployees() {
      for (let key in this.params) {
        if (this.params[key] == '') {
          delete this.params[key]
        }
      }
      let res = await request.get("/baseUser/pageQuery", {
        params: this.params
      })
      this.resData = res.data
    },
    //页面改变
    pageChangeHandler(page) {
      this.params.page = page
      this.pageQueryEmployees()
    },
    //分页数量改变
    sizeChangeHandler(pageSize) {
      this.params.pageSize = pageSize
      this.pageQueryEmployees()
    },
    //     //查询产品
    //     findCategory() {
    //       this.pageQueryEmployees()
    //     },
    addHandler() {
      this.employees = {}
      this.title = '添加员工信息'
      this.visible1 = true
    },
    toSaveHandler() {
      //qs.stringify 转换为表单数据
      this.employees.roleId = 6
      this.$refs['saveForm'].validate((valid) => {
        if (valid) {
          request.post("/baseUser/addUserWidthRole", qs.stringify(this.employees)).then(res => {
            this.pageQueryEmployees()
            this.$message.success(res.message)   //提示操作结果
            this.visible1 = false
          })
        } else {
          return false
        }
      })
    },
    dialogCloseHandler() {
      this.$refs["saveForm"].resetFields(); // 置空表单验证
    },
    dialogCloseTable() {
      this.$refs["saveForm2"].resetFields(); // 置空表单验证
    },
    changeTable(row) {
      this.employees = _.clone(row)   //将当前行数据复制给数据模型
      this.title = '编辑员工信息'
      this.visible2 = true
    },
    deleteTable(id) {
      let r = confirm('确认删除吗？')
      if (r == true) {
        request.get('/baseUser/deleteById', { params: { id } }).then(res => {
          this.pageQueryEmployees(); // 重载数据
          this.$message.success(res.message); // 提示信息
        })
      }
    },
    toSaveTable() {
      this.$refs['saveForm2'].validate((valid) => {
        if (valid) {
          request.post("/baseUser/saveOrUpdate", qs.stringify(this.employees)).then(res => {
            this.pageQueryEmployees()
            this.$message.success(res.message)   //提示操作结果
            this.visible2 = false
          })
        } else {
          return false
        }
      })
    },
    juspTable(id) {
      this.$router.push({
        name:'PeopleDetails',
        params:{
          id:id
        }
      })
    }
  },
  //重新渲染页面
  created() {
    this.pageQueryEmployees()
  },
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>